class RuleTableInstance {
  // 构造函数
  constructor() {
    this.build = this.build.bind(this)
    this.getTableData = this.getTableData.bind(this)
    this.ColmergeData = this.ColmergeData.bind(this)
    this.setHighLight = this.setHighLight.bind(this)
    this.setHighLights = this.setHighLights.bind(this)
    this.getFactor = this.getFactor.bind(this)
    this.setFactorTitle = this.setFactorTitle.bind(this)
    this.getRule = this.getRule.bind(this)
    this.findRule = this.findRule.bind(this)
    this.getRuleResult = this.getRuleResult.bind(this)
    this.findRuleResult = this.findRuleResult.bind(this)
    this.setRuleCondition = this.setRuleCondition.bind(this)
    this.setRuleResult = this.setRuleResult.bind(this)
    this.setRuleData = this.setRuleData.bind(this)
    this.addRule = this.addRule.bind(this)
    this.deleteRule = this.deleteRule.bind(this)
    this.deleteRuleByCondition = this.deleteRuleByCondition.bind(this)
    this.clear = this.clear.bind(this)
  }
  //初始化table实例
  init(ruleTable) {
    this.table = ruleTable
    // this.table.factors=this.table.
  }
  //构建规则表
  build() {
    this.table.$nextTick(() => {
      this.table.generateTable()
    })
  }
  // 获取表格数据
  getTableData() {
    return this.table.data
  }
  // 设置表格数据
  setTableData(data) {
    this.table.data = data
  }
  // 设置高亮-单个设置
  setHighLight({ index, name }) {
    this.table.heilight = [{ index, name }]
  }
  // 设置高亮-多个设置
  setHighLights(heilight) {
    this.table.heilight = heilight
  }
  // 获取因子数据
  getFactor(code) {
    const { colConditions, rowConditions, results } = this.table
    const factors = [...colConditions, ...rowConditions, ...results]
    return factors.find((item) => item.code === code)
  }
  // 修改因子标题
  setFactorTitle(code, title) {
    const { colConditions, rowConditions, results } = this.table
    const factors = [...colConditions, ...rowConditions, ...results]
    factors.find((item) => item.code === code).title = title
    this.table.columns = this.table.buildTableColumns
  }

  // 通过key获取rule
  getRule(key) {
    return this.table.rules.find((item) => (item.key = key))
  }

  // 根据条件查询规则
  findRule(condition, options = {}) {
    const { mode = 'first' } = options
    const keys = Object.keys(condition)
    if (mode === 'first') {
      return this.table.rules.find((rule) => {
        return keys.every((key) => {
          return rule.condition[key] === condition[key] || rule.result[key] === condition[key]
        })
      })
    } else if (mode === 'all') {
      return this.table.rules.filter((rule) => {
        return keys.every((key) => rule.condition[key] === condition[key] || rule.result[key] === condition[key])
      })
    }
  }

  // 获取规则结果
  getRuleResult(key) {
    return this.table.rules.find((rule) => rule.key === key).result
  }

  // 根据条件查询规则结果
  findRuleResult(condition, options = {}) {
    const { mode = 'first' } = options
    const keys = Object.keys(condition)
    if (mode === 'first') {
      return this.table.rules.find((rule) => {
        return keys.every((key) => {
          return rule.condition[key] === condition[key] || rule.result[key] === condition[key]
        })
      })?.result
    } else if (mode === 'all') {
      return this.table.rules
        .filter((rule) => {
          return keys.every((key) => rule.condition[key] === condition[key] || rule.result[key] === condition[key])
        })
        ?.map((item) => item.result)
    }
  }

  //设置规则条件
  setRuleCondition(key, condition) {
    this.table.rules.forEach((rule) => {
      if (rule.key === key) {
        rule.condition = condition
      }
    })
  }
  //设置规则结果
  setRuleResult(key, result) {
    this.table.rules.forEach((rule) => {
      if (rule.key === key) {
        rule.result = result
      }
    })
  }
  //设置规则数据
  setRuleData(key, data) {
    this.table.rules.forEach((rule) => {
      if (rule.key === key) {
        rule.condition = data.condition
        rule.result = data.result
        rule.key = key
      }
    })
    console.log(this.table.rules, 'rulessssss')
  }

  // 添加规则
  addRule(rule) {
    // console.log(this.table.rules,'rules')
    // this.table.rules.push(rule)
    this.table.rules.push(rule)
  }

  // 删除规则
  deleteRule(key) {
    this.table.rules = this.table.rules.filter((item) => item.key != key)
  }

  // 根据条件删除规则
  deleteRuleByCondition(condition) {
    const keys = Object.keys(condition)
    this.table.rules = this.table.rules.filter((rule) => {
      return keys.every((key) => rule.condition[key] === condition[key] || rule.result[key] === condition[key])
    })
  }

  // 清空所有规则result数据
  clear() {
    this.table.rules = this.table.rules.map((rule) => {
      return {
        ...rule,
        result: {},
      }
    })
  }
  // 获取结果
  getResult(code) {
    return this.table.result.find((item) => item.code === code)
  }
  // 设置结果
  setResult(code, title) {
    this.table.result.find((item) => item.code === code).title = title
  }
  // 添加结果
  addResult({ code, title, dataType, render, parser, options } = {}) {
    this.table.result.push({ code, title, dataType, render, parser, options })
  }
  // 删除结果
  deleteResult(result) {
    const Index = this.table.result.findIndex((item) => item !== result)
    if (Index > -1) {
      this.table.result.splice(Index, 1)
    }
  }
  // 设置表格
  ColmergeData({ columns, ColIndex, tableKeys }) {
    return this.table.ColmergeData({ columns, ColIndex, tableKeys })
    // console.log('tableClass', this.table);
  }
}

const RuleTable = {
  createRuleTable: () => new RuleTableInstance(),
}

export default RuleTable
